﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Mail;
using System.Web.Http;

namespace EmailDemo.Controllers
{
    [Authorize]
    public class ValuesController : ApiController
    {
        public async void SendEmailDemo() {
            MailMessage msg = new MailMessage();
            msg.To.Add("275452995@qq.com");
            //msg.To.Add(b@b.com);
            /*  
             msg.To.Add("b@b.com");  
            * msg.To.Add("b@b.com");  
            * msg.To.Add("b@b.com");可以发送给多人  
            */
            //msg.CC.Add(c@c.com);
            /*  
            * msg.CC.Add("c@c.com");  
            * msg.CC.Add("c@c.com");可以抄送给多人  
            */
            msg.From = new MailAddress("729628777@qq.com", "罗宁", System.Text.Encoding.UTF8);
            /* 上面3个参数分别是发件人地址（可以随便写），发件人姓名，编码*/
            msg.Subject = "这是测试邮件";//邮件标题   
            msg.SubjectEncoding = System.Text.Encoding.UTF8;//邮件标题编码   
            msg.Body = "邮件内容";//邮件内容   
            msg.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码   
            msg.IsBodyHtml = false;//是否是HTML邮件   
            msg.Priority = MailPriority.High;//邮件优先级   
            SmtpClient client = new SmtpClient();
            client.Credentials = new NetworkCredential("729628777@qq.com", "a123b456+.0");
            //上述写你的GMail邮箱和密码   
            client.Port = 587;//Gmail使用的端口   
            client.Host = "smtp.gmail.com";
            client.EnableSsl = true;//经过ssl加密   
            object userState = msg;
            try
            {
                client.SendAsync(msg, userState);
                //简单一点儿可以client.Send(msg);   
            }
            catch (SmtpException ex)
            {
            }
        }
    }
}
